/**
 * MainMenuBar.java
 * @author: liuex
 * 2007-12-30 ����07:36:03
 *
 * note: 
 */
package lex.mtd.ui;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.lex.swing.ActionBuilder;
import org.lex.swing.util.Buttons;
import org.lex.swing.util.CenterSouthPane;
import org.lex.swing.widgets.EnhancedDialog;
import org.lex.swing.widgets.MemoryBar;
import org.lex.swing.widgets.SystemPropertiesTable;

import lex.mtd.Download;
import lex.mtd.ui.actions.AboutAction;
import lex.mtd.ui.actions.ExitAction;
import lex.mtd.ui.actions.PreferencesAction;
import lex.mtd.utils.Actions;

public class MainMenuBar extends JMenuBar {
	private JMenu fileMenu;
	private JMenu toolMenu;
	private JMenu helpMenu;
	private JMenu taskMenu;
	private ActionMap am = null;

	public MainMenuBar() {
		super();
		am = Download.getApplication().getContext().getActionMap();
		this.initialize();
	}

	private void initialize() {
		this.add(getFileMenu());
		this.add(getTaskMenu());
		this.add(getToolMenu());
		this.add(getHelpMenu());
	}

	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(new ExitAction());
		}
		return fileMenu;
	}

	private JMenu getTaskMenu() {
		if (taskMenu == null) {
			taskMenu = new JMenu();
			taskMenu.setText("Task");
			taskMenu.add(am.get(Actions.NEW_TASK));
			taskMenu.add(am.get(Actions.START_SELECTED_TASKS));
			taskMenu.add(am.get(Actions.STOP_SELECTED_TASKS));
		}
		return taskMenu;
	}

	private JMenu getToolMenu() {
		if (toolMenu == null) {
			toolMenu = new JMenu();
			toolMenu.setText("Tool");
			toolMenu.add(new PreferencesAction());
		}
		return toolMenu;
	}

	public JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(createMemoryAction());
			helpMenu.add(this.createSystemPropertiesAction());
			helpMenu.addSeparator();
			helpMenu.add(new AboutAction());
		}
		return helpMenu;
	}

	private Window root() {
		return Download.getApplication().getRootWindow();
	}

	private Action createSystemPropertiesAction()
	{
		ActionBuilder a = new ActionBuilder( "System" );
		a.setActionListener( new ActionListener(){
			@Override
			public void actionPerformed( ActionEvent e )
			{
				new SwingWorker<EnhancedDialog, Void>(){
					@Override
					protected EnhancedDialog doInBackground() throws Exception
					{
						EnhancedDialog d = new EnhancedDialog( root() );
						d.setTitle( "System Properties" );
						d.setSize( 450, 450 );
						d.setContentPane( new JScrollPane( new SystemPropertiesTable() ) );
						d.moveToParent();
						return d;
					}

					@Override
					protected void done()
					{
						try
						{
							this.get().setVisible( true );
						}
						catch( Exception e )
						{
							e.printStackTrace();
						}
					}
				}.execute();
			}
		} );
		return a;
	}

	private Action createMemoryAction()
	{
		ActionListener al = new ActionListener(){
			@Override
			public void actionPerformed( ActionEvent e )
			{
				new SwingWorker<EnhancedDialog, Void>(){
					@Override
					protected EnhancedDialog doInBackground() throws Exception
					{
						final EnhancedDialog d = new EnhancedDialog( root() );
						d.setTitle( "Memory" );
						d.setSize( 245, 135 );
						JPanel center = new JPanel();
						center.add( new MemoryBar() );
						Buttons b = Buttons.center( "OK" );
						b.getButton( 0 ).addActionListener( new ActionListener(){
							@Override
							public void actionPerformed( ActionEvent e )
							{
								d.dispose();
							}
						} );
						d.setContentPane( new CenterSouthPane( center, b ) );
						d.setDefaultButton( b.getButton( 0 ) );
						d.moveToParent();
						return d;
					}

					@Override
					protected void done()
					{
						try
						{
							this.get().setVisible( true );
						}
						catch( Exception e )
						{
							e.printStackTrace();
						}
					}
				}.execute();
			}
		};
		ActionBuilder a = new ActionBuilder( "Memory" );
		a.setActionListener( al );
		return a;
	}
}
